package com.zyz.rabbitmqspringboot.controller;

import com.zyz.rabbitmqspringboot.constants.Constants;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/producer")
public class ProducerController {
    @Autowired
    private RabbitTemplate rabbittemplate;

    @RequestMapping("/work")
    public String work() {
        for (int i = 0; i < 10; i++) {
            String msg = "hello spring amqp: work...";
            // 使用内置交换机，发送信息
            rabbittemplate.convertAndSend("", Constants.WORK_QUEUE,msg);
        }
        return "发送成功~";
    }

    @RequestMapping("/fanout")
    public String fanout() {
        rabbittemplate.convertAndSend(Constants.FANOUT_EXCHANGE,"","hello spring amqp:fanout...");
        return "发送成功~";
    }

    @RequestMapping("/direct")
    public String direct(String routingKey) {
        rabbittemplate.convertAndSend(Constants.DIRECT_EXCHANGE,routingKey,"hello spring amqp:direct ," +
                "my routingKey is " + routingKey);
        return "发送成功~";
    }

    @RequestMapping("/topic")
    public String topic(String routingKey) {
        rabbittemplate.convertAndSend(Constants.TOPIC_EXCHANGE,routingKey,"hello spring amqp:topic ," +
                "my routingKey is " + routingKey);
        return "发送成功~";
    }
}
